//
//  LXChangeUserPwdViewController.m
//  cat
//
//  Created by imera on 16/3/16.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXChangeUserPwdViewController.h"
#import "LXNavBarView.h"
#import "AFNetworking.h"
#import "MBProgressHUD+MJ.h"

@interface LXChangeUserPwdViewController() <LXNavBarViewDelegate>
/** 原密码 */
@property (nonatomic, weak) UITextField *oldPasswordTextField;
/** 新密码 */
@property (nonatomic, weak) UITextField *nePasswordTextField;
/** 确认密码 */
@property (nonatomic, weak) UITextField *confirmPasswordTextField;
@end

@implementation LXChangeUserPwdViewController

- (void)viewDidLoad
{
    self.view.backgroundColor = MainBackgroundColor;
    
    [self initSubViews];
}

- (void)initSubViews
{
    /** 导航条 */
    LXNavBarView *navBarView = [[LXNavBarView alloc]init];
    navBarView.leftTitle = @"修改密码";
    navBarView.rightTitle = @"保存";
    navBarView.myDelegate = self;
    navBarView.frame = CGRectMake(0, 20, self.view.frame.size.width, LXNavBarHeight);
    [self.view addSubview:navBarView];
    
    /** 原密码 */
    UITextField *oldPasswordTextField = [[UITextField alloc]init];
    oldPasswordTextField.placeholder = @"原密码";
    oldPasswordTextField.borderStyle = UITextBorderStyleRoundedRect;
    oldPasswordTextField.secureTextEntry = YES;
    self.oldPasswordTextField = oldPasswordTextField;
    [self.view addSubview:oldPasswordTextField];
    
    CGFloat oldPasswordTextFieldX = 10;
    CGFloat oldPasswordTextFieldY = 20 + LXNavBarHeight + 10;
    CGFloat oldPasswordTextFieldW = self.view.frame.size.width - oldPasswordTextFieldX * 2;
    CGFloat oldPasswordTextFieldH = 35;
    oldPasswordTextField.frame = CGRectMake(oldPasswordTextFieldX, oldPasswordTextFieldY, oldPasswordTextFieldW, oldPasswordTextFieldH);
    
    /** 新密码 */
    UITextField *newPasswordTextField = [[UITextField alloc]init];
    newPasswordTextField.placeholder = @"新密码";
    newPasswordTextField.borderStyle = UITextBorderStyleRoundedRect;
    newPasswordTextField.secureTextEntry = YES;
    self.nePasswordTextField = newPasswordTextField;
    [self.view addSubview:newPasswordTextField];
    
    CGFloat newPasswordTextFieldX = 10;
    CGFloat newPasswordTextFieldY = CGRectGetMaxY(oldPasswordTextField.frame) + 10;
    CGFloat newPasswordTextFieldW = oldPasswordTextFieldW;
    CGFloat newPasswordTextFieldH = oldPasswordTextFieldH;
    newPasswordTextField.frame = CGRectMake(newPasswordTextFieldX, newPasswordTextFieldY, newPasswordTextFieldW, newPasswordTextFieldH);
    
    /** 确认密码 */
    UITextField *confirmPasswordTextField = [[UITextField alloc]init];
    confirmPasswordTextField.borderStyle = UITextBorderStyleRoundedRect;
    confirmPasswordTextField.placeholder = @"确认密码";
    confirmPasswordTextField.secureTextEntry = YES;
    self.confirmPasswordTextField = confirmPasswordTextField;
    [self.view addSubview:confirmPasswordTextField];
    
    CGFloat confirmPasswordTextFieldX = 10;
    CGFloat confirmPasswordTextFieldY = CGRectGetMaxY(newPasswordTextField.frame) + 10;
    CGFloat confirmPasswordTextFieldW = oldPasswordTextFieldW;
    CGFloat confirmPasswordTextFieldH = oldPasswordTextFieldH;
    confirmPasswordTextField.frame = CGRectMake(confirmPasswordTextFieldX, confirmPasswordTextFieldY, confirmPasswordTextFieldW, confirmPasswordTextFieldH);
}

#pragma mark - 自定义导航栏代理
- (void)popCurrentViewController:(LXNavBarView *)navBarView
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightBtnClicked:(LXNavBarView *)navBarView
{
    NSString *oldPassword = [[NSUserDefaults standardUserDefaults]objectForKey:@"password"];
    if ([self.oldPasswordTextField.text isEqualToString:@""]) {
        [MBProgressHUD showSuccess:@"原密码不能为空"];
        return;
    }
    if ([self.nePasswordTextField.text isEqualToString:@""]) {
        [MBProgressHUD showSuccess:@"新密码不能为空"];
        return;
    }
    if ([self.confirmPasswordTextField.text isEqualToString:@""]) {
        [MBProgressHUD showSuccess:@"确认密码不能为空"];
        return;
    }
    
    if (![oldPassword isEqualToString:self.oldPasswordTextField.text]) {
        [MBProgressHUD showSuccess:@"原密码不正确"];
        return;
    }
    
    if (![self.confirmPasswordTextField.text isEqualToString:self.nePasswordTextField.text]) {
        [MBProgressHUD showSuccess:@"新密码和确认密码不一致"];
        return;
    }
    
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    
    [mgr POST:@"http://cat666.com/cat666-interface/index.php/index/alterPassword" parameters:@{@"userid":@(self.user.ID), @"password":self.nePasswordTextField.text} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dict = responseObject;
        if([dict[@"successed"] integerValue] == 1){
            [MBProgressHUD showSuccess:@"修改密码成功"];
            
            [[NSUserDefaults standardUserDefaults]setObject:self.nePasswordTextField.text forKey:@"password"];
            
        }else{
            [MBProgressHUD showSuccess:@"修改密码失败"];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD showSuccess:@"修改密码失败"];
    }];
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
@end
